/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.howtimeflies.simplecrypt;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangg on 2015/5/25 0025 21:56.
 *
 * @author <a href=mailto:zhanggeng@howtimeflies.org>GengZhang</a>
 */
public class FileUtils {

    public static List<File> listFile(File parent, boolean recursive, FileFilter fileFilter) {
        List<File> allFile = new ArrayList<File>();
        File[] files = parent.listFiles(fileFilter);
        for (File file : files) {
            if (file.isDirectory() && recursive) {
                listFileRecursive(file, recursive, fileFilter, allFile);
            } else {
                allFile.add(file);
            }
        }
        return allFile;
    }

    private static void listFileRecursive(File parent, boolean recursive, FileFilter fileFilter, List<File> allFile) {
        File[] files = parent.listFiles(fileFilter);
        for (File file : files) {
            if (file.isDirectory() && recursive) {
                listFileRecursive(file, recursive, fileFilter, allFile);
            } else {
                allFile.add(file);
            }
        }
    }

    public static String subExtends(String fileName, int times) {
        for (int i = 0; i < times; i++) {
            fileName = fileName.substring(0, fileName.lastIndexOf("."));
        }
        return fileName;
    }
}
